package com.example.teesystem.common.utils;

import com.example.teesystem.common.cache.CourseCache;
import com.example.teesystem.common.cache.UserCache;
import com.example.teesystem.common.exception.CustomException;
import com.example.teesystem.entity.Permission;
import com.example.teesystem.entity.User;
import com.example.teesystem.filter.MyHandlerInterceptor;
import com.example.teesystem.mapper.AssistantMapper;
import lombok.RequiredArgsConstructor;
import lombok.experimental.UtilityClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class PermissionUtil {
    public static final long CLASS_INFO_MANAGE = 1;
    public static final long STUDENT_INFO_MANAGE = 2;
    public static final long ISSUE_MANAGE = 4;
    public static final long KNOWLEDGE_MANAGE = 8;
    public static final long COURSE_INFO_EDIT = 16;
    public static final long ADVANCED_TRAINING_MANAGE = 32;
    public static final long PAPER_PROVIDE = 64;
    public static final long PAPER_READ = 128;
    public static final long PAPER_RELEASE = 256;
    public static final long STUDY_RESOURCES_MANAGE = 512;
    public static final long LOG_READ = 1024;
    public static final long ACCOUNT_CREATE = 2048;
    public static final long ASSISTANT_MANAGER = 4096;
    public static final long NUM = 13;

    @Autowired
    private AssistantMapper assistantMapper1;
    private static AssistantMapper assistantMapper;

    @PostConstruct
    public void init(){
        assistantMapper = assistantMapper1;
    }

    /**
     * 获取当前权限的map
     * @param permissions
     * @return
     */
    public static Map<Integer, String> getAssistantPermissionMap(long permissions) {
        Map<Integer, String> map = new HashMap<Integer, String>();
        int start = 1;
        for (int i = 0; i < NUM; i++) {
            if(verify(permissions,start)){
                map.put(start,getPermission(start));
            }
            start*=2;
        }
        return map;
    }

    public static String getPermission(long permission) {
        if (permission == CLASS_INFO_MANAGE) {
            return "班级信息管理";
        } else if (permission == STUDENT_INFO_MANAGE) {
            return "学生信息管理";
        } else if (permission == ISSUE_MANAGE) {
            return "题库管理";
        } else if (permission == KNOWLEDGE_MANAGE) {
            return "知识点管理";
        } else if (permission == COURSE_INFO_EDIT) {
            return "课程信息管理";
        } else if (permission == ADVANCED_TRAINING_MANAGE) {
            return "进阶训练管理";
        } else if (permission == PAPER_PROVIDE) {
            return "出卷";
        } else if (permission == PAPER_READ) {
            return "阅卷";
        } else if (permission == PAPER_RELEASE) {
            return "已阅试卷发布";
        } else if (permission == STUDY_RESOURCES_MANAGE) {
            return "学习资料管理";
        } else if (permission == LOG_READ) {
            return "课程日志查看";
        } else if (permission == ACCOUNT_CREATE) {
            return "账户创建";
        } else if (permission == ASSISTANT_MANAGER) {
            return "助教管理";
        } else {
            return "默认权限";
        }

    }

    /**
     * 获得我的权限
     * @param Permission
     * @return
     */
    public static List<Permission> getMyPermission(long Permission){
        long permission = 0;
        List<Permission> myPermissions = new ArrayList<>();
        int PermissionNum = 0;
        long start = 1;
        for(int i = 0 ; i < NUM ; i ++){
            permission = Permission&start;
            start=start<<1;
            if(getPermission(permission)!="默认权限"){
                myPermissions.add(new Permission(permission,getPermission(permission)));
                PermissionNum++;
            }
        }
        if(PermissionNum==0) {
            myPermissions.add(new Permission(0,"默认权限"));
        }
        return myPermissions;

    }

    /**
     *权限验证
     * @param myPermissions
     * @param permission
     * @return
     */
    public static boolean verify(long myPermissions, long permission){
        return (myPermissions & permission) == permission;
    }

    /**
     * 是否是管理员
     * @return
     */
    public static Boolean isManager(){
        User user = UserCache.userMap.get(MyHandlerInterceptor.getUid());
        return user.getType() == 0;
    }

    /**
     * @param uid
     * @param permission
     * @param courseId
     * @return
     */
    public static Boolean permissionCheck(int uid,long permission,int courseId){
        if(!CourseCache.courseMap.containsKey(courseId)){
            throw new CustomException(ResultCode.PARAMETER_ERROR.getMsg(),ResultCode.PARAMETER_ERROR.getCode());
        }
        if(CourseCache.courseMap.get(courseId).getTeacher() == uid){
            return true;
        }else {
            long myPermission = 0;
            myPermission = assistantMapper.getAssistantPermission(uid,courseId);
            return PermissionUtil.verify(myPermission, permission);
        }

    }
}
